package com.example.springsecurity.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;

@Configuration
public class BeanConfig {

    @Autowired
    private DataSource dataSource;

    //进行自定义登录逻辑时容器内必须要有PasswordEncoder实例
    @Bean
    public PasswordEncoder getPw(){
        return  new BCryptPasswordEncoder();
    }

    @Bean
    public PersistentTokenRepository getPersistentTokenRepository(){
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setDataSource(dataSource);
        //当启动时自动建表,第二次启动一定要注释掉
//        jdbcTokenRepository.setCreateTableOnStartup(true);
        return  jdbcTokenRepository;
    }
}
